﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Support_UserManage : BasicPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DEPT_NO_A.DataSource = GetPubData("Dep").Tables[0].DefaultView;
            DEPT_NO_A.DataTextField = "DATA_NAME";
            DEPT_NO_A.DataValueField = "DATA_CODE";
            DEPT_NO_A.DataBind();
            SetPager();
        }
    }

    private void SetPager()
    {
        PagedDataSource pds = new PagedDataSource();
        DataSet ds = GetUser();
        UserPager.RecordCount = ds.Tables[0].Rows.Count;
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = UserPager.PageSize;
        pds.CurrentPageIndex = UserPager.CurrentPageIndex - 1;
        UserList.DataSource = pds;
        UserList.DataBind();
    }

    private DataSet GetUser()
    {
        string sqlStr = "SELECT ULOGIN,UNAME,t2.data_name AS DEP,ROLE,IFVALID FROM USER_INFO t1" +
                        " LEFT JOIN  PUBLIC_DATA t2 ON t1.DEP = t2.data_code AND t2.data_type='Dep' ";
        if (ULogin_I.Text.Trim().Length > 0)
        {
            sqlStr = sqlStr + " WHERE ULOGIN LIKE '%" + ReplaceString(ULogin_I.Text.Trim(), 16) + "%'";
        }
        sqlStr = sqlStr + " ORDER BY t2.data_code,IFVALID DESC";
        return OracleHelper.ExecuteDataSet(sqlStr, CommandType.Text, null);
    }

    protected void BtnCreate_Click(object sender, EventArgs e)
    {
        if (ULoginOnly())
        {
            AddUser();
            ResetAddForm();
            SetPager();
            Page.ClientScript.RegisterStartupScript(this.GetType(), "success"
                , "$(function() {alert('用户已新增成功。');})", true);
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "fail"
                , "$(function() {$('#AddUserDiv').dialog('open');alert('登录名已存在，请重新输入。');})", true);
        }
    }

    private void ResetAddForm()
    {
        User_Login_A.Text = "";
        Real_Name_A.Text = "";
        DEPT_NO_A.SelectedIndex = 0;
        Role_A.SelectedIndex = 0;
    }

    private bool ULoginOnly()
    {
        string sqlstr = "SELECT COUNT(1) FROM USER_INFO WHERE ULOGIN = :ULOGIN";
        OracleParameter[] _params = {
                new OracleParameter(":ULOGIN",OracleType.VarChar,20)
            };
        _params[0].Value = User_Login_A.Text.Trim();
        return OracleHelper.ExecuteScalar(sqlstr, CommandType.Text, _params).ToString().Equals("0");
    }

    private int AddUser()
    {
        string sqlStr = "insert into USER_INFO (ULOGIN,UPASS,UNAME,DEP,ROLE,IFVALID)"
                        + " values(:ULOGIN,:UPASS,:UNAME,:DEP,:ROLE,:IFVALID)";

        OracleParameter[] _param = {
			new OracleParameter(":ULOGIN",OracleType.VarChar,20),
			new OracleParameter(":UPASS",OracleType.VarChar,200),
            new OracleParameter(":UNAME",OracleType.VarChar,20),
			new OracleParameter(":DEP",OracleType.Number),
			new OracleParameter(":ROLE",OracleType.VarChar,20),
			new OracleParameter(":IFVALID",OracleType.VarChar,2)
			};

        _param[0].Value = User_Login_A.Text.Trim();
        _param[1].Value = DefaultPass;
        _param[2].Value = Real_Name_A.Text.Trim();
        _param[3].Value = DEPT_NO_A.SelectedValue;
        _param[4].Value = Role_A.SelectedValue;
        _param[5].Value = "Y";

        return OracleHelper.ExecuteNonQuery(sqlStr, CommandType.Text, _param);
    }

    protected void UserList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Charge")
        {
            if (IsOnlyAdmin(e.CommandArgument.ToString()))
            {
                JQueryAlert("唯一已启用系统管理员禁止禁用！");
            }
            else
            {
                ModifyStatus(e.CommandArgument.ToString());
            }
            SetPager();
        }
        else if (e.CommandName == "Reset")
        {
            ChangePassword(e.CommandArgument.ToString());
            Page.ClientScript.RegisterStartupScript(this.GetType(), "success"
                , "$(function() {alert('密码重置成功。');})", true);
        }
    }

    private int ModifyStatus(string user_login)
    {
        string sqlStr = "UPDATE USER_INFO SET IFVALID=DECODE(IFVALID,'Y','N','Y') WHERE ULOGIN =:user_login";
        OracleParameter[] _params = {
                new OracleParameter(":user_login",OracleType.VarChar)
            };
        _params[0].Value = user_login;
        return OracleHelper.ExecuteNonQuery(sqlStr, CommandType.Text, _params);
    }

    private bool IsOnlyAdmin(string arg)
    {
        int count = -1;
        string sqlStr = "SELECT count(1) FROM USER_INFO WHERE IFVALID = 'Y' AND ROLE = '系统管理' AND ULOGIN <> :ulogin";
        OracleParameter[] _params = {
                    new OracleParameter(":ulogin",OracleType.VarChar)
            };
        _params[0].Value = arg;
        count = Int32.Parse(OracleHelper.ExecuteScalar(sqlStr, CommandType.Text, _params).ToString());
        return (count == 0);
    }

    private int ChangePassword(string arg)
    {
        int effRow = 0;
        string sqlStr = "UPDATE USER_INFO SET UPASS =:USER_PASS WHERE ULOGIN =:ULOGIN";
        OracleParameter[] _params = {
                new OracleParameter(":USER_PASS",OracleType.VarChar,200),
                new OracleParameter(":ULOGIN",OracleType.VarChar,20)
            };
        _params[0].Value = DefaultPass;
        _params[1].Value = arg;
        effRow = OracleHelper.ExecuteNonQuery(sqlStr, CommandType.Text, _params);
        return effRow;
    }

    protected void UserPager_PageChanged(object sender, EventArgs e)
    {
        SetPager();
    }
    protected void QueryUser_Click(object sender, EventArgs e)
    {
        UserPager.CurrentPageIndex = 1;
        SetPager();        
    }
}
